<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
        <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
        <script src="/js/plugins/dialog/dialog.min.js"></script>
        <script src="/js/plugins/jquery-form/jquery.form.js"></script>
		<script src="/js/plugins/jrender/jrender.js"></script>
		<script type="text/javascript">
			$(function () {
                //从本地session中取出缓存的对象,渲染到界面中
                var user = JSON.parse(sessionStorage.getItem("user"));
                $(".info").renderValues(user);
                $(".myBg").css("background-image", "url('"+user.coverImgUrl+"')");

                //给保存按钮绑定点击事件把更新的用户信息发PUT请求到服务器
                $("#saveBtn").click(function () {
                    $.ajax({
                        url: "/users/"+user.id,
                        data: $("#infoForm").serialize(),
                        type: "PUT",
                        success: function (data) {
                            //1:更新本地缓存
                            data.data.email = user.email;
                            sessionStorage.setItem("user", JSON.stringify(data.data));
                            $(document).dialog({
                                type: "confirm",
                                style: "IOS",
                                titleShow: false,
                                content: "更新成功",
                                buttons: [{
                                    name: "返回个人主页",
                                    callback: function() {
                                        //2:回到个人信息界面
                                        location.href = "/mine/profiles.html";
                                    }
                                }]
                            });
                        }
                    });
                });

                var flag = true; //默认选择换头像
                //处理图片上传
                $("#headImg,#headBtn").click(function () {
                    flag = true;
                    //弹出文件上传的选择器,使用代码模拟点击
                    $("#imgForm input[name='pic']").click();
                });

                $("#coverBtn").click(function () {
                    flag = false;
                    //弹出文件上传的选择器,使用代码模拟点击
                    $("#imgForm input[name='pic']").click();
                });

                //设置文件上传值改变事件
                $("#imgForm input[name='pic']").change(function () {
                    //判断是否有选择图片
                    if ($("#imgForm input[name='pic']").val()) {
                        //使用ajax提交图片上传的表单
                        $("#imgForm").ajaxSubmit(function (data) {
                            if (flag) {
                                //回显到当前页面中(1:头像/2:隐藏域)
                                $("#headImg").attr("src", data.url);
                                $("#infoForm input[name='headImgUrl']").val(data.url);
                            } else {
                                //回显到当前页面中(1:背景/2:隐藏域)
                                $(".myBg").css("background-image", "url('"+data.url+"')");
                                $("#infoForm input[name='coverImgUrl']").val(data.url);
                            }
                        });
                    }

                });
            });
		</script>
	</head>

	<body>
		<form id="imgForm" action="/images" method="post" enctype="multipart/form-data">
			<input type="file" name="pic" style="display: none;" accept="image/*"/>
		</form>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a role="button" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" >
					<i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i>
				</div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" >
                <!-- 隐藏当前用户的图片地址 -->
				<input type="hidden" name="headImgUrl" render-value="headImgUrl">
				<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
				<label>昵称</label>
				<input type="text" name="nickName" class="form-control" render-value="nickName">
				<label>性别</label>
				<select class="form-control" name="gender" render-value="gender">
					<option value="0">保密</option>
					<option value="1">男</option>
					<option value="2">女</option>
				</select>
				<label>地区</label>
				<input type="text" name="place" class="form-control" render-value="place">
				<label>签名</label>
				<textarea name="sign" class="form-control" rows="5" render-html="sign"></textarea>
			</form>
		</div>

	</body>
</html>
